﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.CommonObject;

namespace ZBJF.Warranty.BLL.Cached
{
    public class AppConfigCached
    {
        public static AppConfigEntity GetAppConfig()
        {
            try
            {
                AppConfigEntity appconfig = new AppConfigEntity
                {
                    SMSUrl = GetConfigValue("SMSUrl"),
                    SMSUserName = GetConfigValue("SMSUserName"),
                    SMSPassword = GetConfigValue("SMSPassword"),

                    UploadFileVirtualPath=GetConfigValue("UploadFileVirtualPath"),

                    #region 平安
                    PinanAPIUrl = GetConfigValue("PinanAPIUrl"),
                    PinanClientID = GetConfigValue("PinanClientID"),
                    PinanClientSecret = GetConfigValue("PinanClientSecret"),
                    PinanAssessToken = GetConfigValue("PinanAssessToken"),
                    #endregion

                    #region 微信
                    WeiXin_AppId = GetConfigValue("WeiXin_AppId"),
                    WeiXin_Token = GetConfigValue("WeiXin_Token"),
                    WeiXin_AppSecret = GetConfigValue("WeiXin_AppSecret"),
                    WeiXin_Key = GetConfigValue("WeiXin_Key"),
                    WeiXin_MchId = GetConfigValue("WeiXin_MchId"),
                    WeiXin_NotifyUrl = GetConfigValue("WeiXin_NotifyUrl"),
                    WeiXin_SSLCertPassword = GetConfigValue("WeiXin_SSLCertPassword"),
                    WeiXin_SSLCertPath = GetConfigValue("WeiXin_SSLCertPath"),
                    WeiXin_EncodingAESKey = GetConfigValue("WeiXin_EncodingAESKey"),
                    ServiceSiteUrl = GetConfigValue("ServiceSiteUrl"),
                    H5SiteUrl = GetConfigValue("H5SiteUrl"),
                    #endregion

                    #region 华安
                    HuaAnSecretKey = GetConfigValue("HuaAnSecretKey"),
                    #endregion

                    #region 顺丰
                    Sf_appid = GetConfigValue("FengQiao_clientCode"),
                    Sf_appkey = GetConfigValue("Sf_appkey"),
                    #endregion

                    #region 丰桥
                    FengQiao_clientCode = GetConfigValue("FengQiao_clientCode"),
                    FengQiao_checkWord = GetConfigValue("FengQiao_checkWord"),
                    #endregion
                };
                return appconfig;
            }
            catch (Exception exception)
            {
                return new AppConfigEntity();
            }
        }

        public static void ClearAppConfigCache()
        {
            CacheHelper.Instance.RemoveCache("AppConfigCache");
        }
        private static string GetConfigValue(string configKey)
        {
            var appConfig = GetConfig.FirstOrDefault(o => o.ConfigKey.Equals(configKey));
            return appConfig == null ? string.Empty : appConfig.ConfigValue;
        }

        private static List<AppSettings> _appConfig;
        private static List<AppSettings> GetConfig
        {
            get
            {
                return _appConfig ?? (_appConfig = CacheHelper.Instance.GetDataFromCache("AppConfigCache", () => ServiceFactory.AppSettingsServiceInstance.LoadEntities(p => true).ToList()));
            }
        }


        public static ApiPlatformConfig GetApiPlatformConfig(string keyValue)
        {
            return ApiPlatformConfig.FirstOrDefault(c => c.AppKeyValue == keyValue);
        }
        private static List<ApiPlatformConfig> _apiPlatformConfig;
        private static List<ApiPlatformConfig> ApiPlatformConfig
        {
            get
            {
                return _apiPlatformConfig ?? (_apiPlatformConfig = CacheHelper.Instance.GetDataFromCache("ApiPlatformConfig", () => ServiceFactory.ApiPlatformConfigServiceInstance.LoadEntities(p => true).ToList()));
            }
        }
    }
}
